package com.example.lottery_system.service.activitystatus.operator;

import com.example.lottery_system.dao.DataObject.ActivityUserDO;
import com.example.lottery_system.dao.Mapper.ActivityUserMapper;
import com.example.lottery_system.service.dto.DrawPrizeStatusDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class UserOperator extends AbstractActivityOperator{
    @Override
    public Integer sequence() {
        return 1;
    }

    @Autowired
    ActivityUserMapper activityUserMapper;

    @Override
    public Boolean needConvert(DrawPrizeStatusDto drawPrizeStatusDto) {
        if (null == drawPrizeStatusDto) {
            log.warn("needConvert::drawPrizeStatusDto为空!");
            return false;
        }


//        判空
        List<Long> userIds = drawPrizeStatusDto.getUserIds();
        if (null == drawPrizeStatusDto.getActivityId() || CollectionUtils.isEmpty(userIds)) {
            log.warn("needConvert::userIds或activityId为空!");
            return false;
        }

//        获取到用户成员
        List<ActivityUserDO> userDoList = activityUserMapper.selectByIds(drawPrizeStatusDto.getActivityId(),userIds);

        if (userDoList == null) {
            log.warn("查询到的中奖成员为空");
            return false;
        }

        for (ActivityUserDO activityUserDo: userDoList) {
//            只要有状态不需要改变的，就直接返回 false
            if (drawPrizeStatusDto.getTargetUserStatus().name().equalsIgnoreCase(activityUserDo.getStatus())) {
                return false;
            }
        }


        return true;
    }

    @Override
    public Boolean convert(DrawPrizeStatusDto drawPrizeStatusDto) {
        try {
            activityUserMapper.updateUserStatus(drawPrizeStatusDto.getActivityId()
                    ,drawPrizeStatusDto.getUserIds()
                    ,drawPrizeStatusDto.getTargetUserStatus().name());
            return true;
        } catch (Exception e) {
            log.warn("convert::drawPrizeStatusDto数据库转化失败!");
            return false;
        }

    }
}
